package com.rowline.admin.module.study.dynamicproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ClassName:CglibDynamicProxyDemo
 * Created by 245167
 * on 2018/5/2  02:33
 * description:
 */
public class CglibDynamicProxyDemo implements MethodInterceptor{

    public Object getProxy(Class clazz){
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        // 设置需要创建子类的类
        enhancer.setSuperclass(clazz);
        // 通过字节码技术动态创建子类实例
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("前置通知");
        Object result = methodProxy.invokeSuper(o,objects);
        System.out.println("后置通知");
        return result;
    }

    public static void main(String[] args) {
//        CglibDynamicProxyDemo proxy = new CglibDynamicProxyDemo();
//        RealSubject realSubject = (RealSubject) proxy.getProxy(RealSubject.class);
//        realSubject.hello("hello kitty");
//        realSubject.rent();

        Map<String,String> concurrentHashMap = new ConcurrentHashMap<String,String>();
        concurrentHashMap.put("1","kitty");
        String conValue = concurrentHashMap.get("1");
        System.out.println("concurrentHashMap key=1 的 value 为：" + conValue);
        concurrentHashMap.size();


        Map<String,String> hashmap = new HashMap<String,String>();
        hashmap.put("1","ketty");
        String value = hashmap.get("1");
        System.out.println("hashMap key=1 的 value 为：" + value);
    }
}
